import json
from json import decoder

import requests

url = 'http://shop-xo.hctestedu.com'
# url = 'http://shop-xo.hctestedu.com?s=/api/user/login'
headers = {"s": "/api/user/login"}
params = {"s": "/api/user/login",
          "application": "app"}
data = {"accounts": "peipei_001", "pwd": "123456", "type": "username"}

# login_response = requests.post(url=url,headers=headers, data=data, params=params)
login_response = requests.post (url=url, data=data, params=params)
print (login_response.text)

login_res = login_response.text
json_data = json.loads (login_res)
res_data = json_data["data"]
print (json_data)
res_msg = json_data["msg"]
print (res_msg)
res_data = json_data["data"]
print (res_data)

res_token = res_data["token"]
print ("返回的token值为:", res_token)
# return(res_token)

# def add_favorite(res_token):
url = 'http://shop-xo.hctestedu.com'
params = {"s": "/api/goods/favor",
          "application": "app",
          "token": res_token
          }
data = {"id": "12"}

add_favorite_response = requests.post (url=url, params=params, json=data)
print ("商品收藏正确：", add_favorite_response.text)
res_data = add_favorite_response.text
print (type(res_data))
res_jason = json.loads(res_data)
print (res_jason)
print (res_jason["msg"])
#res_json =
#print (res_json)


url = 'http://shop-xo.hctestedu.com'
params = {"s": "api/cart/save",
          "application": "app",
          "token": res_token
          }
data = {"goods_id":"2","stock":"3"}
add_favorite_response = requests.post (url=url, params=params, json=data)
print ("商品加入正确：", add_favorite_response.text)
res_data = add_favorite_response.text
res_jason = json.loads(res_data)
print (res_jason)
print (res_jason["msg"])
#res_json =
#print (res_json)